/*
 * $RCSfile: Processor_46_Msg.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.db.UserInfoDao;
import com.work.db.imp.UserInfoDaoImp;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 *
 * 充值成功后更新用户数据
 * <p>Title: Processor_62_Persion</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_65_UpdateUserData extends Handler{

	private Logger logger = Logger.getLogger(Processor_65_UpdateUserData.class);
	public byte[] handleRequest() {
		logger.debug("Processor_65_UpdateUserData userId is : " + this.getGameRequest().getUserId());
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int userid = request.getUserId();
		try {
			UserInfo userInfo = Cache.getOnlineUserInfo().get(userid);
			if (userInfo == null) {
				return pusUser(1);
			}
			Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
			if (room == null) {
				return pusUser(1);
			}
			RoomPersion p = room.getAudMap().get(userid);
			if (p == null) {
				return pusUser(1);
			}
			//更新用户信息
			UserInfoDao dao = new UserInfoDaoImp();
			UserInfo newUserInfo = dao.getUserInfo(request.getUserId());
			int addChip = newUserInfo.getChip() -  userInfo.getChip();
			userInfo.setChip(newUserInfo.getChip());
			p.setChouma(p.getChouma() + addChip);
			userInfo.setUserProductByList(newUserInfo.getUserProductByList());
			return pusUser(0);
		} catch (Exception e) {
			logger.debug("发送消息出错", e);
			return pusUser(1);
		}
	}

	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_UPDATE_USER_DATA);
		return bytes;
	}

	public Object makeObject() throws Exception {
		return new Processor_65_UpdateUserData();
	}

}

